package cn.schoolwow.workflow.module.task.action.flow.common;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployApprovalType;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowNodeJump;
import cn.schoolwow.workflow.entity.WorkFlowTask;

import java.util.List;

public class SetNextNodeCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        hiddenWorkFlowTask(flowContext);
        setNextNode(flowContext);
    }

    @Override
    public String name() {
        return "查找并设置下一个流程节点";
    }

    private void hiddenWorkFlowTask(FlowContext flowContext){
        Condition condition = null;
        if(flowContext.containInstanceKey(Transaction.class)){
            Transaction transaction = flowContext.checkInstanceData(Transaction.class);
            condition = transaction.query(WorkFlowTask.class);
        }else{
            DAO dao = flowContext.checkInstanceData(DAO.class);
            condition = dao.query(WorkFlowTask.class);
        }
        WorkFlowNode currentWorkflowNode = flowContext.checkData("currentWorkflowNode", WorkFlowNode.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        if(null==currentWorkflowNode.getApprovalType()){
            return;
        }
        DeployApprovalType deployApprovalType = DeployApprovalType.getDeployApprovalType(currentWorkflowNode.getApprovalType());
        if(!DeployApprovalType.AnyApproval.equals(deployApprovalType)){
            return;
        }

        condition.addQuery("instanceId", workFlowInstance.getId())
                .addQuery("nodeId", currentWorkflowNode.getId())
                .addQuery("complete", false)
                .addUpdate("hidden", true)
                .execute()
                .update();
    }

    private void setNextNode(FlowContext flowContext){
        WorkFlowNode currentWorkflowNode = flowContext.checkData("currentWorkflowNode", WorkFlowNode.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        TaskType taskType = flowContext.checkInstanceData(TaskType.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        dao.addRecordString("查询当前节点的后继节点记录");
        List<WorkFlowNodeJump> workFlowNodeJumpList = dao.query(WorkFlowNodeJump.class)
                .addQuery("definitionId", workFlowInstance.getDefinitionId())
                .addQuery("currentNodeId", currentWorkflowNode.getId())
                .addQuery("condition", taskType.code)
                .execute()
                .getList();
        if(null==workFlowNodeJumpList||workFlowNodeJumpList.isEmpty()){
            throw new IllegalArgumentException("当前节点的后续节点不存在!当前节点名称:"+currentWorkflowNode.getName());
        }
        if(workFlowNodeJumpList.size()>1){
            throw new IllegalArgumentException("当前节点的满足条件的后续节点存在["+workFlowNodeJumpList.size()+"]个!当前节点名称:"+currentWorkflowNode.getName());
        }

        WorkFlowNode nextNode = dao.fetch(WorkFlowNode.class, workFlowNodeJumpList.get(0).getNextNodeId());
        flowContext.putTemporaryData("nextNode", nextNode);
    }
}
